<?php

namespace app\controller\admin\cms;

use app\BaseController;
use think\facade\Request;
use app\model\cms\CategoryModel;
use app\model\cms\ArticleModel;


class Article extends BaseController
{

    public function index()
    {

        $list = ArticleModel::get_list();

        foreach ($list as &$item) {
            $item['public_time'] = Date('Y-m-d', strtotime($item['public_time']));
        }
        return json($list);
    }


    public function single()
    {

        $id = Request::param('id');
        $id = $id ? intval( $id ) : 0;
        $res = [
            'status' => 0
        ];
        if (!$id) {
            $res['status'] = 1;
            $res['description'] = '参数不合法';
        }
        $article = ArticleModel::single($id);
        $article['public_time'] = Date('Y-m-d', strtotime($article['public_time']));
        $article['content'] = html_entity_decode($article['content'], ENT_QUOTES, 'UTF-8');
        $article['content'] = str_replace('-jinghao-', '#', $article['content']);
        $article['custom'] = !empty($article['custom']) ? json_decode( $article['custom'], true ) : [];
        return json($article);
    }

    public function edit()
    {

        // global $_GPC;
        $_GPC = Request::param();
        $id = $_GPC['id'] ? intval($_GPC['id']) : 0;

        if (empty($_POST['title'])) {

            return json([
                'status' => 1,
                'description' => '请填写文章标题',
            ]);
            die();
        }

        $time = Date('Y-m-d H:i:s');
        // todo 获取 分类路径
        // $content = strip_tags( htmlentities( $_GPC['content'] ,ENT_QUOTES, 'UTF-8' ));
        $content =  !empty($_GPC['content']) ? $_GPC['content'] : '';
        $content = str_replace('#', '-jinghao-', $content);
        $content = strip_tags( htmlentities( $_GPC['content'] ,ENT_QUOTES, 'UTF-8' ));

        $data = [
            'title'         => $_GPC['title'],
            'subtitle'      => !empty($_GPC['subtitle']) ? $_GPC['subtitle'] : '',
            'category_id'   => !empty($_GPC['category_id']) ? intval($_GPC['category_id']) : 0,
            'public_time'   => !empty($_GPC['public_time']) ? $_GPC['public_time'] : '',
            'author'        => !empty($_GPC['author']) ? $_GPC['author'] : '',
            'content'       => $content,
            // 'custom'        => !empty($_GPC['custom']) ? json_decode($_GPC['custom'], true) : [],
            'custom'        => !empty($_GPC['custom']) ? $_GPC['custom'] : '[]',
            'out_link'      => !empty($_GPC['out_link']) ? $_GPC['out_link'] : '',
            'sort_order'    => $_GPC['sort_order'] ? intval($_GPC['sort_order']) : 0,
            'image'         => $_GPC['image'] ? $_GPC['image'] : '',
            'is_recommend'  => $_GPC['is_recommend'] ? intval($_GPC['is_recommend']) : 0,
            'viewtimes'     => $_GPC['viewtimes'] ? intval($_GPC['viewtimes']) : 0,
            'updated_at'    => $time,
        ];
        //todo 修改 category

        if ($id) {
            $ret = ArticleModel::update2($id, $data);
        } else {
            $data['created_at'] = $time;
            $id = $ret = ArticleModel::insert($data);
        }

        if (!$ret || !$id) {
            $res['status'] = 1;
            $res['description'] = '保存失败';
            return json($res);
            die();
        }


        $res['status'] = 0;
        $res['ret'] = $ret;
        return json($res);
    }

    public function delete()
    {

        $_GPC = Request::param();
        $res = [
            'status' => 0,
        ];
        $id = $_GPC['id'] ? intval($_GPC['id']) : 0;

        $res['ret'] = ArticleModel::delete2($id);

        return json($res);
    }
}
